Ext.onReady(function() {

    // 元数据的store
    var vehicleusestore = new Ext.data.Store({
        storeId: 'store',
        proxy: new Ext.data.HttpProxy({
            url: 'metadata.action?code=vehicleuse'
        }),
        reader: new Ext.data.JsonReader({
            fields: [{
                    name: 'name'
                }, {
                    name: 'value'
                }]
        })
    });

    var storecompany = new Ext.data.Store({
        storeId: 'storecompany',
        proxy: new Ext.data.HttpProxy({
            url: 'metadata.action?code=company'
        }),
        reader: new Ext.data.JsonReader({
            fields: [{
                    name: 'name'
                }, {
                    name: 'value'
                }]
        })
    });

    var storedepartment = new Ext.data.Store({
        storeId: 'storedepartment',
        proxy: new Ext.data.HttpProxy({
            url: 'metadata.action?code=department'
        }),
        reader: new Ext.data.JsonReader({
            fields: [{
                    name: 'name'
                }, {
                    name: 'value'
                }]
        })
    });

    var storetype = new Ext.data.Store({
        storeId: 'storetype',
        proxy: new Ext.data.HttpProxy({
            url: 'metadata.action?code=type'
        }),
        reader: new Ext.data.JsonReader({
            fields: [{
                    name: 'name'
                }, {
                    name: 'value'
                }]
        })
    });


    var storeattributes = new Ext.data.Store({
        storeId: 'storeattributes',
        proxy: new Ext.data.HttpProxy({
            url: 'metadata.action?code=attributes'
        }),
        reader: new Ext.data.JsonReader({
            fields: [{
                    name: 'name'
                }, {
                    name: 'value'
                }]
        })
    });

    var storelogo = new Ext.data.Store({
        storeId: 'storelogo',
        proxy: new Ext.data.HttpProxy({
            url: 'metadata.action?code=logo'
        }),
        reader: new Ext.data.JsonReader({
            fields: [{
                    name: 'name'
                }, {
                    name: 'value'
                }]
        })
    });

    var BasicInformation = new Ext.FormPanel({
        id: 'BasicInformation',
        name: 'BasicInformation',
        labelWidth: 80,
        width: '100%',
        height: '100%',
        bodyStyle: 'padding: 4px',
        border: false,
        autoscroll: true,
        frame: true,
        autoScroll: true,
        items: [{
                layout: 'column',
                items: [{
                        columnWidth: .5,
                        layout: 'form',
                        items: [{
                                xtype: 'hidden',
                                name: 'id'
                            }, {
                                xtype: 'textfield',
                                fieldLabel: '车牌号码',
                                emptyText: '车牌号码(必填)',
                                allowBlank: false,
                                name: 'code',
                                anchor: '95%'
                            }, new Ext.form.ComboBox({
                                fieldLabel: '资产属性',
                                hiddenName: 'attributes',
                                store: storeattributes,
                                displayField: 'name',
                                valueField: 'name',
                                typeAhead: true,
                                triggerAction: 'all',
                                emptyText: '---请选择---',
                                allowBlank: false,
                                anchor: "95%",
                                name: 'attributes',
                                selectOnFocus: true
                            })]
                    }, {
                        columnWidth: .5,
                        layout: 'form',
                        items: [
                            new Ext.form.ComboBox({
                                fieldLabel: '产权单位名称',
                                hiddenName: 'company',
                                store: storecompany,
                                displayField: 'name',
                                valueField: 'name',
                                typeAhead: true,
                                triggerAction: 'all',
                                emptyText: '---请选择---',
                                allowBlank: false,
                                anchor: "95%",
                                name: 'company',
                                selectOnFocus: true
                            }),
                            new Ext.form.ComboBox({
                                fieldLabel: '使用部门名称',
                                hiddenName: 'department',
                                store: storedepartment,
                                displayField: 'name',
                                valueField: 'name',
                                typeAhead: true,
                                triggerAction: 'all',
                                emptyText: '---请选择---',
                                allowBlank: false,
                                anchor: "95%",
                                name: 'department',
                                selectOnFocus: true
                            })]
                    }, {
                        columnWidth: .5,
                        layout: 'form',
                        items: [new Ext.form.ComboBox({
                                fieldLabel: '车辆类型',
                                hiddenName: 'type',
                                store: storetype,
                                displayField: 'name',
                                valueField: 'name',
                                typeAhead: true,
                                triggerAction: 'all',
                                emptyText: '---请选择---',
                                allowBlank: false,
                                anchor: "95%",
                                name: 'type',
                                selectOnFocus: true
                            }), {
                                xtype: 'textfield',
                                fieldLabel: '车辆原值',
                                emptyText: '车辆原值(万)(必填)',
                                allowBlank: false,
                                name: 'price',
                                anchor: '95%'
                            }]
                    }, {
                        columnWidth: .5,
                        layout: 'form',
                        items: [{
                                xtype: 'textfield',
                                fieldLabel: '制作厂商',
                                emptyText: '制作厂商(必填)',
                                allowBlank: false,
                                name: 'manufacturer',
                                anchor: '95%'
                            }, {
                                xtype: 'textfield',
                                fieldLabel: '驾驶员姓名',
                                name: 'driver',
                                anchor: '95%'
                            }]
                    }, {
                        columnWidth: .5,
                        layout: 'form',
                        items: [{
                                xtype: 'textfield',
                                fieldLabel: '品牌',
                                emptyText: '品牌(必填)',
                                allowBlank: false,
                                name: 'brand',
                                anchor: '95%'
                            }, {
                                xtype: 'textfield',
                                fieldLabel: '排量',
                                emptyText: '排量(必填)',
                                allowBlank: false,
                                name: 'displacement',
                                anchor: '95%'
                            }]
                    }, {
                        columnWidth: .5,
                        layout: 'form',
                        items: [{
                                xtype: "radiogroup",
                                fieldLabel: "使用公司标识",
                                disabled: false,
                                id: 'logo',
                                name: 'logo',
                                anchor: "95%",
                                items: [{
                                        boxLabel: '是',
                                        name: 'logo',
                                        inputValue: '1'
                                    }, {
                                        boxLabel: '否',
                                        name: 'logo',
                                        inputValue: '2',
                                        checked: true
                                    }]
                            }, {
                                xtype: "radiogroup",
                                fieldLabel: "是否安装GPS",
                                disabled: false,
                                id: 'gps',
                                name: 'gps',
                                anchor: "95%",
                                items: [{
                                        boxLabel: '是',
                                        name: 'gps',
                                        inputValue: '1'
                                    }, {
                                        boxLabel: '否',
                                        name: 'gps',
                                        inputValue: '2',
                                        checked: true
                                    }]
                            }]
                    }, {
                        columnWidth: .5,
                        layout: 'form',
                        items: [{
                                xtype: 'datefield',
                                fieldLabel: '购车日期',
                                name: 'purchase_date',
                                emptyText: '购车日期(必填)',
                                allowBlank: false,
                                altFormats: 'Ymd',
                                format: 'Y-m-d',
                                dateFormat: 'Ymd',
                                anchor: '95%'
                            }, {
                                xtype: 'datefield',
                                fieldLabel: '使用日期',
                                name: 'use_date',
                                altFormats: 'Ymd',
                                format: 'Y-m-d',
                                dateFormat: 'Ymd',
                                anchor: '95%'
                            }]

                    }]

            }]
    });

    var BasicInformation_add_win = new Ext.Window({
        id: 'BasicInformation_win',
        title: "添加车辆基本信息",
        border: false,
        bodyBorder: false,
        // layout:'fit',
        width: 630,
        closeAction: 'hide',
        modal: true,
        tbar: ['-', {
                text: ' 提交并继续',
                handler: function() {
                    onSubmit();
                }
            }, '-', {
                text: ' 重置',
                handler: function() {
                    Ext.getCmp("BasicInformation").getForm().reset();
                }
            }, '-'],
        items: [BasicInformation]
    }).show();

    function onSubmit() {
        // 提交表单
        if (BasicInformation.getForm().isValid()) {
            var submitValues = BasicInformation.getForm().getValues();
            for (var param in submitValues) {
                if (BasicInformation.form.findField(param)
                        && BasicInformation.form.findField(param).emptyText == submitValues[param]) {
                    BasicInformation.form.findField(param).setValue(' ');
                }
            }
            BasicInformation.getForm().submit({
                url: 'BasicInformationAction.action',
                waitMsg: '数据保存中...',
                submitEmptyText: false,
                success: function(c, d) {
                    Ext.MessageBox.alert("操作信息", "成功信息保存！");
                    BasicInformation.getForm().reset();
                },
                failure: function(c, d) {
                    Ext.MessageBox.show({
                        title: "操作信息",
                        msg: "信息保存出错，请联系管理员！",
                        buttons: Ext.MessageBox.OK,
                        icon: "ext-mb-error"
                    });

                }
            });

        }

    }
})